package com.yc.zookeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat;

public class ZooKeeperProSync implements Watcher {
	
	private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
	private static ZooKeeper zk = null;
	private static Stat stat = new Stat();
	
	public static void main(String[] args) throws Exception {
		//zookeeper配置数据存放路径
		String path = "/yc/us";
		//连接zookeeper并注册一个默认的监听器
		zk = new ZooKeeper("172.16.67.127:2181,172.16.67.128:2181,172.16.67.122:2181", 3000, new ZooKeeperProSync());
		//等待zk连接成功的通知
		connectedSemaphore.await();
		//获取path目录节点的配置数据，并注册默认的监听器
		System.out.println(new String(zk.getData(path, true, stat)));
		
		Thread.sleep(Integer.MAX_VALUE);
		
	}

	@Override
	public void process(WatchedEvent event) {
		if (KeeperState.SyncConnected == event.getState()) {//zk连接成功通知事件
			if (EventType.None == event.getType() && null == event.getPath()) {
				connectedSemaphore.countDown();
			} else if (event.getType() == EventType.NodeDataChanged) {//节点数据变化
				try {
					System.out.println("配置已修改，新值为：" + new String(zk.getData(event.getPath(), true, stat)));
				} catch (Exception e) {
					
				}
			}
			
		}
		
	}

}
